package com.saas.tenant.context;

import com.alibaba.ttl.TransmittableThreadLocal;

/**
 * 租户上下文持有者
 * 采用 Alibaba TTL 保证线程池/异步线程间传递
 */
public final class TenantContextHolder {

    /**
     * TTL 线程变量
     */
    private static final TransmittableThreadLocal<Long> TENANT_CONTEXT = new TransmittableThreadLocal<>();

    private TenantContextHolder() {
    }

    /**
     * 设置当前租户ID
     */
    public static void setTenantId(Long tenantId) {
        TENANT_CONTEXT.set(tenantId);
    }

    /**
     * 获取当前租户ID
     */
    public static Long getTenantId() {
        return TENANT_CONTEXT.get();
    }

    /**
     * 清除当前线程租户ID
     */
    public static void clear() {
        TENANT_CONTEXT.remove();
    }
}